package com.ailot.cloud.auth.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

import cn.hutool.system.UserInfo;
import com.ailot.cloud.auth.feign.FeignUpmsService;
import com.ailot.cloud.auth.service.JwtUserDetailsService;
import com.ailot.cloud.base.common.constants.SecurityConstant;
import com.ailot.cloud.base.common.dto.Result;
import com.ailot.cloud.base.common.exception.BizException;
import com.ailot.cloud.base.security.model.JwtUser;
import com.ailot.cloud.upms.common.dto.JwtUserDTO;
import com.ailot.cloud.upms.common.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

/**
 * 用户详细信息
 *
 * @author
 */
@Slf4j
@AllArgsConstructor
@Service
public class JwtUserDetailsServiceImpl implements JwtUserDetailsService {

    private final FeignUpmsService feignUserService;

    /**
     * 用户密码登录
     *
     * @param username 用户名
     */
    @SneakyThrows
    @Override
    public UserDetails loadUserByUsername(String username) {
        Result<JwtUserDTO> result = feignUserService.getInfo(username);
        if (result.isOk()){
            JwtUserDTO jwtUserDTO = result.getData();
            return getUserDetails(jwtUserDTO);
        }else {
         throw new BizException("用户名或密码错误");
        }
    }

    /**
     * 手机验证码登录
     *
     * @param phone 手机号
     * @return UserDetails
     */
    @Override
    @SneakyThrows
    public UserDetails loadUserByPhone(String phone) {
        return null;
    }

    /**
     * 构建UserDetails
     */
    private UserDetails getUserDetails(JwtUserDTO jwtUserDTO) {
        if (jwtUserDTO == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        Set<String> dbAuthsSet = new HashSet<>();
        if (ArrayUtil.isNotEmpty(jwtUserDTO.getRoles())) {
            // 获取角色
            Arrays.stream(jwtUserDTO.getRoles()).forEach(roleId -> dbAuthsSet.add(SecurityConstant.ROLE + roleId));
            // 获取资源
            dbAuthsSet.addAll(Arrays.asList(jwtUserDTO.getPermissions()));
        }
        // 构建用户的权限信息
        // 由角色和普通权限组合成完整权限list
        Collection<? extends GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));

        SysUser user = jwtUserDTO.getSysUser();

        // 构造security用户
        return new JwtUser(user.getId(), user.getOrgId(), user.getUsername(), user.getPassword(), authorities);
    }
}

